package com.guet.niit.Login.controller.user;

import com.guet.niit.User.pojo.UserCar;
import com.guet.niit.User.service.UserService;
import com.sun.javafx.stage.StageHelper;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

public class StationInfoFxController implements Initializable {

    public static StationInfoFxController stationInfoFxController;

    @FXML
    private ImageView backtrack;

    @FXML
    private Text chargingCar;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        stationInfoFxController = this;
    }

    /**
     * 返回充电站导航
     * @param mouseEvent
     */
    public void backtrack(MouseEvent mouseEvent) {
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/user/selectChargingStations.fxml"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取当前窗口，并将Scene更换为用户菜单的Scene
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));
    }

    /**
     * 选择充电车辆
     * @param mouseEvent
     */
    public void addChargingCar(MouseEvent mouseEvent) {
        //获取当前登录用户的id
        String name = Thread.currentThread().getName();
        long userId = Long.parseLong(name);
        //调用service层查询用户拥有的汽车
        UserService userService = new UserService();
        ArrayList<UserCar> userCars = userService.ReturnCarResult(userId);
        //创建可选择对话框的选项容器
        List<String> choices = new ArrayList<>();
        String defaultChoice = "";
        //添加可选择对话框的选项
        if (userCars.size()>0){
            for (int i = 0; i < userCars.size(); i++) {
                choices.add(userCars.get(i).getCarName()+" "+userCars.get(i).getLicenseNumber());
                defaultChoice = userCars.get(0).getCarName()+" "+userCars.get(0).getLicenseNumber();
            }
        }else {
            choices.add("您的账户下还没有车辆，请先添加");
            defaultChoice = "您的账户下还没有车辆，请先添加";
        }
        //创建可选择对话框
        ChoiceDialog<String> dialog = new ChoiceDialog<>(defaultChoice,choices);
        dialog.setTitle("选择充电汽车");
        dialog.setHeaderText("");
        dialog.setContentText("请选择您要充电的汽车:");
        Optional<String> result = dialog.showAndWait();
        //用户点击确定
        if (result.isPresent()){
            //用户有可选择的车辆，则显示充电汽车的信息
            if (!(result.get().equals("您的账户下还没有车辆，请先添加"))){
                chargingCar.setText(result.get());
            }
        }
    }

    /**
     * 提交订单
     * @param mouseEvent
     */
    public void submitOrder(MouseEvent mouseEvent) {
        //获取当前登录用户的id
        String name = Thread.currentThread().getName();
        long userId = Long.parseLong(name);

        //获取用户本次消费额


        //获取用户余额
        UserService userService = new UserService();
        Double balance = userService.ReturnBalace(userId);

        //判断用户是否能成功消费


        //获取用户选择充电的车辆
        String selectCar = chargingCar.getText();

        //调用service层提交订单
        //1.更新用户余额

        //2.往charging_record表插入记录

    }
}
